<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>子传父 通过自定义事件</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
    <!-- 父组件模板 -->
    <div id="app">
     <cpn @item-click="cpnClick"></cpn>
    </div>

    <!-- 子组件模板 -->
   <template id="cpn">
       <div>
          <button v-for="item in categories" @click="btnClick(item)">
              {{item.name}}
            </button>
       </div>
   </template>

    <script>
        //1. 子组件
       const cpn = {
           template: '#cpn',
           data() {
                return {
                    categories: [
                        {id: 'aaa', name: '热门推荐'},
                        {id: 'bbb', name: '手机数码'},
                        {id: 'ccc', name: '家用家电'},
                        {id: 'ddd', name: '电脑办公'}
                    ]
                }
           },
           methods: {
                btnClick(item) {
                    // 发送事件 ，由父组件v-on监听 自定义事件
                    this.$emit('item-click', item)
                }
           }
       }
      
       //2. 父组件
        const app = new Vue({
            el: "#app",
            data: {
              info: {
                  name: 'DIO',
                  age: 200,
                  height: 1.88
              }
            
            },
            components: {
              cpn
            },
            methods: {
                cpnClick(item) {
                    console.log('cpnClick', item)
                }
            }
        })
    </script>
</body>
</html>